import { PORTS, getServiceUrls } from '../../config/ports.js';

// 获取当前环境的服务地址
export const getCurrentServiceUrls = () => {
  const hostname = window.location.hostname;
  return getServiceUrls(hostname);
};

// 获取WebSocket连接地址
export const getWebSocketUrl = () => {
  const hostname = window.location.hostname;
  return `ws://${hostname}:${PORTS.WEBSOCKET}`;
};

// 获取API基础地址
export const getApiBaseUrl = () => {
  const hostname = window.location.hostname;
  return `http://${hostname}:${PORTS.HTTP}`;
};

// 获取服务配置
export const getServiceConfig = async () => {
  try {
    const response = await fetch(`${getApiBaseUrl()}/serviceConfig`);
    if (!response.ok) {
      throw new Error(`HTTP ${response.status}: ${response.statusText}`);
    }
    return await response.json();
  } catch (error) {
    console.error('获取服务配置失败:', error);
    throw error;
  }
};

export default {
  getCurrentServiceUrls,
  getWebSocketUrl,
  getApiBaseUrl,
  getServiceConfig,
};
